package com.cskaoyan.user.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.user.converter.UserConverterMapper;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.dto.UserCheckLoginResponse;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/21 11:29
 * @Description:
 */
@Service
public class LoginService implements ILoginService {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserConverterMapper userConverterMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;

    @Override
    public UserLoginResponse login(UserLoginRequest request) {
        String userName = request.getUserName();
        String userPwd = request.getUserPwd();
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        // 与数据库数据比对
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("username", userName).andEqualTo("password", md5DigestAsHex);
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() == 0) {
            throw new BizException(SysRetCodeConstants.INCORRECT_CREDENTIAL.getCode(), SysRetCodeConstants.INCORRECT_CREDENTIAL.getMessage());
        }
        // 判断是否激活
        for (Member member : members) {
            String isVerified = member.getIsVerified();
            if (!"Y".equals(isVerified)) {
                throw new BizException(SysRetCodeConstants.USER_NOT_VERIFIED.getCode(), SysRetCodeConstants.USER_NOT_VERIFIED.getMessage());
            }
        }
        // 封装返回信息
        Member member = members.get(0);
        UserLoginResponse userLoginResponse = userConverterMapper.converter(member);
        return userLoginResponse;
    }

    @Override
    public UserCheckLoginResponse checkLogin(Long uid) {
        Member member = memberMapper.selectByPrimaryKey(uid);
        UserCheckLoginResponse userCheckLoginResponse = userConverterMapper.memberConvert2UserCheckLoginResponse(member);
        return userCheckLoginResponse;
    }

    @Override
    public void verify(String uuid, String username) {
        // verify表
        UserVerify userVerify = new UserVerify();
        userVerify.setIsVerify("Y");
        Example example = new Example(UserVerify.class);
        example.createCriteria().andEqualTo("uuid", uuid);
        int affectedRows = userVerifyMapper.updateByExampleSelective(userVerify, example);
        if (affectedRows == 0) {
            throw new BizException(SysRetCodeConstants.DB_EXCEPTION.getCode(), SysRetCodeConstants.DB_EXCEPTION.getMessage());
        }
        // member表
        Member member = new Member();
        member.setIsVerified("Y");
        member.setUpdated(new Date());
        Example example1 = new Example(Member.class);
        example.createCriteria().andEqualTo("username", username);
        int affectedRows1 = memberMapper.updateByExampleSelective(member, example1);
        if (affectedRows1 == 0) {
            throw new BizException(SysRetCodeConstants.DB_EXCEPTION.getCode(), SysRetCodeConstants.DB_EXCEPTION.getMessage());
        }
    }


}
